#include "dialoglogin.h"
#include "mainwindow.h"
#include "ui_dialoglogin.h"
#include <QMouseEvent>
#include <QMessageBox>
#include "value.h"
#include "custom.h"
#include "admin_0.h"
#include "dlcw.h"
#include "zczh.h"

Accp ff()               //将存账号的文件里的内容输出到链表中
{
    FILE* fp;       //文件指针
    int n = 0;
    Accp head = NULL;
    Accp p2, p;
    fp = fopen("account", "a+");     //以只读的方式打开文件
    if (fp == NULL)
    {
        //printf("cannot open file\n");
    }
    else
    {
        while (!feof(fp))        //判断文件位置标志是否移动到文件末尾
        {
            n++;
            p = (Accp)malloc(N);
            fread(p, N, 1, fp);
            if (n == 1)
            {
                head = p;
                p2 = p;
            }
            else             //创建链表
            {
                p2->next = p;
                p2 = p;
            }
        }
        p2->next = NULL;
    }
    fclose(fp);

    return head;
}
void save_account(Accp p1,zczh *z)     //将p1所指向的内容存储到文件中
{
    FILE* fp;       //文件指针
    fp = fopen("account", "a");    //以追加的方式打开文件
    if (fp == NULL)
    {
        QMessageBox::warning(z,"err","打开文件失败");
    }
    if (fwrite(p1, N, 1, fp) != 1)    //将p1所指向的1个大小为N的数据结构写入fp所指向的文件中
    {
        QMessageBox::warning(z,"err","文件写入失败");
    }
    fclose(fp);        //关闭文件
}
IAEP ss()             //将存账单的文件里的内容输出到链表中
{
    FILE* fp;              //文件指针
    int n = 0;
    IAEP head = NULL;
    IAEP p, p2, pr = NULL;
    fp = fopen(_name.c_str(), "ab+");//打开文件，方式为只读
    if (fp == NULL)            //如果打开为空
    {
        //printf("cannot open file\n");
    }
    else
    {
        while (!feof(fp))         // feof函数可以检查文件读写位置标志是否移动到文件末尾，feof(fp)检查fp所指向的文件是否结束，如果是
        {                        //则函数值为真，否则为假，即“!feof(fp)”为真，在while循环中检查 “!feof(fp)”为真，就执行循环体
            n++;
            p = (IAEP)malloc(M);   //向内存申请一段空间
            fread(p, M, 1, fp);      //从fp所指向的文件读入1个大小为M的数据，存储到p所指向的空间中
            if (n == 1)
            {
                head = p;
                p2 = p;
                head->pre = NULL;

            }
            else                //创建双向链表
            {
                pr = p2;          //pr是p的前驱结点
                p->pre = p2;
                p2->next = p;
                p2 = p;
            }
        }
        if (pr != NULL)
        {
            pr->next = NULL;
        }
    }
    fclose(fp);
    if (n == 1)
        head = NULL;
    return head;
}
DialogLogin::DialogLogin(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogLogin)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_DeleteOnClose);
    setWindowFlags(Qt::SplashScreen);
    //DialogLogin::adUsername="admin123";
    //DialogLogin::adPswd="123456";
}

DialogLogin::~DialogLogin()
{
    delete ui;
}

void DialogLogin::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton){
        m_moving=true;
        m_lastPos=event->globalPos()-pos();
    }
    return QDialog::mousePressEvent(event);
}

void DialogLogin::mouseMoveEvent(QMouseEvent *event)
{
    if(m_moving&&(event->buttons()&&Qt::LeftButton)){
        move(event->globalPos()-m_lastPos);
        m_lastPos=event->globalPos()-pos();
    }
    return QDialog::mouseMoveEvent(event);
}

void DialogLogin::mouseReleaseEvent(QMouseEvent *event)
{
    m_moving=false;
}

void DialogLogin::on_okBtn_clicked()//确认按钮
{
    Accp qp, head;
    head = ff();
    qp = head;
    QString user=ui->Username->text().trimmed();
    QString pswd=ui->Password->text().trimmed();
    extern std::string _name;
    _name=user.toStdString();
    extern std::string _password;
    _password=pswd.toStdString();
    std::string adminname=adUsername;
    std::string adminpassword=adPswd;
    if (strcmp(_name.c_str(), adminname.c_str()) == 0 && strcmp(_password.c_str(), adminpassword.c_str()) == 0) {
        //ad_menu();
        LoginFlag=1;
        accept();
    }
    else {
        while (qp != NULL && strcmp(qp->name, _name.c_str()) != 0)   //遍历链表，寻找当前输入的账户的账号
            {
                qp = qp->next;
            }
            if (qp != NULL)   //如果p不为空
            {
                if (strcmp(qp->password, _password.c_str()) == 0)     //如果输入的密码正确
                {
                    LoginFlag=2;
                    accept();    //跳出循环
                }
                else      //如果输入的密码错误
                {
                    QMessageBox::warning(this,"err","账号或密码错误");
                }
            }
            else              //如果p为空，即输入的账号不正确
            {
                   QMessageBox::warning(this,"err","此账号不存在，请检查!");
            }
    }
}

void DialogLogin::on_pushButton_clicked()//注册账号
{
    zczh *z=new zczh(this);
    z->setModal(true);
    z->show();
}
